package com.ljz.common.core.controller;


import com.ljz.common.core.exception.BusinessException;
import com.ljz.common.core.exception.ValidateException;
import com.ljz.common.utils.ReturnResult;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 * 利用RestControllerAdvice注解捕获全局异常
 */
@RestControllerAdvice
public class GlobalExceptionController extends MyBaseController {

    //@Value("${spring.servlet.multipart.max-file-size}")
    double maxFileSize;
    /**
     * 捕获自定义的验证异常
     * @param e
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public ReturnResult validateException(ValidateException e) {
        e.printStackTrace();
        return fail("验证异常",e.getError());
    }

    /**
     * 捕获保存异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ReturnResult businessException(BusinessException e) {
        e.printStackTrace();
        return fail(e.getMessage());
    }

    @ExceptionHandler(FileSizeLimitExceededException.class)
    public ReturnResult handlerFileSizeLimitExceededException(FileSizeLimitExceededException e) {
        double size = maxFileSize / 1024 / 1024;
        e.printStackTrace();
        return fail("文件大小不能超过" + size + "M");
    }
}
